﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using WCFMINERP.DAL;
using WCFMINERP.MODEL;
using Newtonsoft.Json;
using fastJSON;

namespace WCFMINERP.BLL.ShopBLL
{
    public class ShopProductBll
    {

        public List<ShopModels> GetShopList(int shopid)
        {
            List<ShopModels> ShopAll = new List<ShopModels>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> listshop = db.Shop.Where(s=>s.Status==1 && s.ShopId==shopid).ToList();
                foreach (Shop shop in listshop)
                {
                    ShopModels model = new ShopModels();
                    model.ShopID = shop.ShopId;
                    model.ShopName = shop.shopName;
                    if (!shop.Shop_Product_Price.IsLoaded)
                    {
                        shop.Shop_Product_Price.Load();
                    }
                    List<Shop_Product_Price> listProd = shop.Shop_Product_Price.ToList();
                    foreach(Shop_Product_Price prod in listProd){
                        prod.Tb_ProductReference.Load();
                        PrdouctModels mode=new PrdouctModels();
                        mode.ProductId = prod.ProductId;                        
                        mode.ProductName = prod.Tb_Product.ProductName;
                        prod.Tb_Product.paperTypeReference.Load();
                        mode.PaperTypeID = prod.Tb_Product.paperType.PaperTypeID;
                        model.ProuctList.Add(mode);
                    }
                    if (!shop.Shop_Paper_Price.IsLoaded)
                    {
                        shop.Shop_Paper_Price.Load();
                    }
                    List<Shop_Paper_Price> listPrice = shop.Shop_Paper_Price.ToList();
                    foreach (Shop_Paper_Price pric in listPrice)
                    {
                        paperTypeModels mode = new paperTypeModels();
                        pric.paperTypeReference.Load();
                        mode.PaperTypeID = pric.PaperTypeId;
                        mode.Name = pric.paperType.Name;
                        if (model.paperTypeList == null) {
                            model.paperTypeList = new List<paperTypeModels>();
                        }
                        model.paperTypeList.Add(mode);
                    }

                    ShopAll.Add(model);
                }
            }
            return ShopAll;

            //if (ShopAll.Count > 0)
            //{
            //    string jsonStr = string.Empty;
            //    //DataContractJsonSerializer json = new DataContractJsonSerializer(ShopAll.GetType());
            //    ////序列化
            //    //using (MemoryStream stream = new MemoryStream())
            //    //{

            //    //    json.WriteObject(stream, ShopAll);
            //    //    jsonStr = Encoding.UTF8.GetString(stream.ToArray());

            //    //}
              
            //    var jsonParameters = new JSONParameters();
            //    jsonParameters.SerializeToLowerCaseNames = true;
            //    jsonParameters.EnableAnonymousTypes = true;
            //    jsonParameters.UseExtensions = false;
            //    jsonParameters.SerializeNullValues = false;
            //    jsonParameters.UsingGlobalTypes = false;
            //    jsonParameters.InlineCircularReferences = true;
            //    jsonParameters.UseEscapedUnicode = false;
            //    JSON.Parameters = jsonParameters;
            //    jsonStr = JSON.ToJSON(ShopAll);

            //    return jsonStr;
            //}
            //else
            //{
            //    return "未找信息";
            //}
        }

        public List<ShopModels> GetShopList()
        {
            List<ShopModels> ShopAll = new List<ShopModels>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> listshop = db.Shop.Where(s => s.Status == 1).ToList();
                foreach (Shop shop in listshop)
                {
                    ShopModels model = new ShopModels();
                    model.ShopID = shop.ShopId;
                    model.ShopName = shop.shopName;
                    if (!shop.Shop_Product_Price.IsLoaded)
                    {
                        shop.Shop_Product_Price.Load();
                    }
                    List<Shop_Product_Price> listProd = shop.Shop_Product_Price.ToList();
                    foreach (Shop_Product_Price prod in listProd)
                    {
                        prod.Tb_ProductReference.Load();
                        PrdouctModels mode = new PrdouctModels();
                        mode.ProductId = prod.ProductId;
                        mode.ProductName = prod.Tb_Product.ProductName;

                        model.ProuctList.Add(mode);
                    }
                    if (!shop.Shop_Paper_Price.IsLoaded)
                    {
                        shop.Shop_Paper_Price.Load();
                    }
                    List<Shop_Paper_Price> listPrice = shop.Shop_Paper_Price.ToList();
                    foreach (Shop_Paper_Price pric in listPrice)
                    {
                        paperTypeModels mode = new paperTypeModels();
                        pric.paperTypeReference.Load();
                        mode.PaperTypeID = pric.PaperTypeId;
                        mode.Name = pric.paperType.Name;
                        if (model.paperTypeList == null)
                        {
                            model.paperTypeList = new List<paperTypeModels>();
                        }
                        model.paperTypeList.Add(mode);
                    }

                    ShopAll.Add(model);
                }
            }

            return ShopAll;

            //string jsonStr = string.Empty;
            ////DataContractJsonSerializer json = new DataContractJsonSerializer(ShopAll.GetType());
            //////序列化
            ////using (MemoryStream stream = new MemoryStream())
            ////{

            ////    json.WriteObject(stream, ShopAll);
            ////    jsonStr = Encoding.UTF8.GetString(stream.ToArray());

            ////}

            //var jsonParameters = new JSONParameters();
            //jsonParameters.SerializeToLowerCaseNames = true;
            //jsonParameters.EnableAnonymousTypes = true;
            //jsonParameters.UseExtensions = false;
            //jsonParameters.SerializeNullValues = false;
            //jsonParameters.UsingGlobalTypes = false;
            //jsonParameters.InlineCircularReferences = true;
            //jsonParameters.UseEscapedUnicode = false;
            //JSON.Parameters = jsonParameters;
            //jsonStr = JSON.ToJSON(ShopAll);

            //return jsonStr;
        }

        public List<PaperFaceModels> GetPaperFace()
        {
            List<PaperFaceModels> PaperFaceModelsAll = new List<PaperFaceModels>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<paperFace> list = db.paperFace.Where(p => p.Status == 1).ToList();
                foreach (paperFace item in list)
                {
                    PaperFaceModels model = new PaperFaceModels();
                    model.PaperFaceID = item.PaperFaceID;
                    model.remark = item.remark;
                    model.Name = item.Name;
                    model.Status = item.Status;

                    PaperFaceModelsAll.Add(model);
                }

                return PaperFaceModelsAll;

                //string jsonStr = string.Empty;
                ////DataContractJsonSerializer json = new DataContractJsonSerializer(PaperFaceModelsAll.GetType());
                //////序列化
                ////using (MemoryStream stream = new MemoryStream())
                ////{

                ////    json.WriteObject(stream, PaperFaceModelsAll);
                ////    jsonStr = Encoding.UTF8.GetString(stream.ToArray());

                ////}
                //var jsonParameters = new JSONParameters();
                //jsonParameters.SerializeToLowerCaseNames = true;
                //jsonParameters.EnableAnonymousTypes = true;
                //jsonParameters.UseExtensions = false;
                //jsonParameters.SerializeNullValues = false;
                //jsonParameters.UsingGlobalTypes = false;
                //jsonParameters.InlineCircularReferences = true;
                //jsonParameters.UseEscapedUnicode = false;
                //JSON.Parameters = jsonParameters;
                //jsonStr = JSON.ToJSON(PaperFaceModelsAll);

                //return jsonStr;
            }
        }
    }
}
